<?php
/**
 * Created by PhpStorm.
 * User: gqn
 * Date: 2016/3/4
 * Time: 10:01
 */

namespace Api\Controller;

Class VolumeController extends BaseController{

    /**
     * 我的优惠券
     */

    public function index(){
        //解决跨域
        echoHeader();
        /** status = 1:未使用 2：已使用 3：已过期**/
        $status = I('post.status','');
        $user_id = I('post.user_id','');
        $data = self::checkUserIsExist($user_id);

        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        if($status =='' || $user_id==''){
            $array = array('err_code'=>1001,'err_msg'=>'参数缺失');
            $this->ajaxReturn($array);
        }
        $not = M('volume_list')->where("user_id = $user_id and status = 1")->select();
        $notcount = count($not);

        $used = M('volume_list')->where("user_id = $user_id and status = 2")->select();
        $usedcount = count($used);

        $past = M('volume_list')->where("user_id = $user_id and status = 3")->select();
        $pastcount = count($past);

        $data = M('volume_list')->where("user_id = $user_id and status = $status")->select();
        foreach($data as $i=>$v){
            $data[$i]['valid_date'] = date('Y-m-d',$data[$i]['start_time']).'至'.date('Y-m-d',$data[$i]['end_time']);
            $data[$i]['start_time'] = date('Y-m-d',$data[$i]['start_time']);
            $data[$i]['end_time'] = date('Y-m-d',$data[$i]['end_time']);
            $data[$i]['add_time'] = date('Y-m-d',$data[$i]['add_time']);
            if($data[$i]['use_time']!='0'){
                $data[$i]['use_time'] = date('Y-m-d',$data[$i]['use_time']);
            }
        }

        if(empty($data)){
            $array = array('err_code'=>'1009','not'=>$notcount,'used'=>$usedcount,'past'=>$pastcount,'err_msg'=>'暂无数据');
            $this->ajaxReturn($array);
        }
        $array = array('err_code'=>'200','not'=>$notcount,'used'=>$usedcount,'past'=>$pastcount,'err_msg'=>$data);
        $this->ajaxReturn($array);
    }

    /**
     * 产品配件
     */
    public function ProductAcc(){
        //1产品 2配件
        $type = I('post.type','');
        if(empty($type)){
            $array = array('err_code'=>'403','err_msg'=>'参数缺失');
            $this->ajaxReturn($array);
        }
        $data = M('Product_acc')->field('name,icon,price,url')->where("type = $type")->select();
        if($data){
            $array = array('err_code'=>'200','err_msg'=>$data);
            $this->ajaxReturn($array);
        }else{
            $array = array('err_code'=>'402','err_msg'=>'暂无数据');
            $this->ajaxReturn($array);
        }
    }

}